import moment from 'moment'
class GetPublicDate {
  // date array dateType 日周月的状态
  // date日 week 周 month 月
  constructor (date, dateType) {
    this.dateList = date
    this.dateType = dateType
  }
  // 返回日
  _setDay () {
    const date = this.dateList.map(v => {
      return v ? moment(v).format('YYYY-MM-DD') : ''
    })
    return date
  }
  // 返回周
  _setWeek () {
    const week = this.dateList.map(v => {
      const item = moment(v).isoWeekYear() + '' + moment(v).format('WW')
      return v ? item : ''
    })
    return week
  }
  // 返回月
  _setYear () {
    const month = this.dateList.map(v => {
      const item = moment(v).format('YYYY-MM').replace('-', '')
      return v ? item : ''
    })
    return month
  }
  // 日区间 val范围界限
  _isLtDay (val) {
    const member = Math.abs(moment(this.dateList[0]).diff(moment(this.dateList[1]),
      this.dateType === 'date' ? 'days' : this.dateType))
    return member > val
  }
  // 返回日表头数据
  _getTableHeaderDay () {
    const startDate = moment(this.dateList[0])
    const endDate = moment(this.dateList[1])
    const member = []
    let diff = endDate.diff(startDate, 'days')
    for (let i = 0; i <= diff; i++) {
      const firstDate = moment(this.dateList[0])
      const dateDay = firstDate.add(i, 'd').format('YYYY-MM-DD')
      member.push({
        label: `${dateDay}(${firstDate.format('dddd')})`,
        name: dateDay,
        field: dateDay,
        align: 'center'
      })
    }
    return member
  }
  // 返回周表头数据
  _getTableHeaderWeek (val) {
    const startDate = moment(this.dateList[0])
    const endDate = moment(this.dateList[1])
    const member = []
    let diff = endDate.diff(startDate, 'week')
    for (let i = 0; i <= diff; i++) {
      const firstDate = moment(this.dateList[0])
      const date = firstDate.add(i, 'week')
      const year = date.startOf('week').format('YYYY')
      const weekStart = date.startOf('week').format('MM-DD')
      const weekEnd = date.endOf('week').format('MM-DD')
      const label = date.format('WW')
      const item = `${year}${label}`
      member.push({
        label: `第${label}周 (${weekStart}~${weekEnd})`,
        name: item,
        field: item,
        align: 'left'
      })
    }
    return member
  }
  // 返回月数据
  _getTableHeaderMonth (val) {
    const startDate = moment(this.dateList[0])
    const endDate = moment(this.dateList[1])
    const member = []
    let diff = endDate.diff(startDate, 'month')
    for (let i = 0; i <= diff; i++) {
      const firstDate = moment(this.dateList[0])
      const date = firstDate.add(i, 'month').format('YYYY-MM')
      member.push({
        label: date,
        name: date.replace('-', ''),
        field: date.replace('-', ''),
        align: 'left'
      })
    }
    return member
  }
  _setReasonDate () {
    let member
    switch (this.dateType) {
      case 'date':
        member = this._setDay()
        break
      case 'week':
        member = this._setDay()
        break
      case 'month':
        member = this.dateList.map(v => {
          const item = moment(v).format('YYYY-MM')
          return v ? item : ''
        })
    }
    return member
  }
}
export default GetPublicDate
